// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   UserTxAction.java

package com.iphaeton.lms.action.organize;

import com.iphaeton.common.excel.IExcelService;
import com.iphaeton.common.helper.UserSession;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.lms.business.organize.*;
import com.iphaeton.lms.business.role.IRoleService;
import com.iphaeton.lms.pojo.organize.Organize;
import com.iphaeton.lms.pojo.organize.User;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.log4j.Logger;
import org.apache.struts.action.*;
import org.dom4j.io.XMLWriter;

// Referenced classes of package com.iphaeton.lms.action.organize:
//            OrganizeBizSupportAction

public class UserTxAction extends OrganizeBizSupportAction
{

    public UserTxAction()
    {
    }

    public ActionForward userForm(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        List post;
        List duty;
        try
        {
            post = group.getGroupList("g", "");
            duty = group.getGroupList("z", "");
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("post", post);
        request.setAttribute("duty", duty);
        request.setAttribute("id", id);
        return mapping.findForward("user_create");
    }

    public ActionForward editUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String page = request.getParameter("page");
        String id = request.getParameter("id");
        String state = request.getParameter("state");
        String src = request.getParameter("src");
        User u = null;
        try
        {
            u = user.findUserById(id);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("listState", state);
        request.setAttribute("pageId", page);
        request.setAttribute("user", u);
        request.setAttribute("src", src);
        return mapping.findForward("userEdit");
    }

    public ActionForward userDetail(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        User u = null;
        try
        {
            u = user.findUserByUserId(id);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("user", u);
        return mapping.findForward("user_detail");
    }

    public ActionForward updateUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        daf = (DynaActionForm)form;
        String src = request.getParameter("src");
        String page = request.getParameter("page");
        User u = null;
        String id = (String)daf.get("id");
        String name = (String)daf.get("name");
        String email = (String)daf.get("email");
        String address = (String)daf.get("address");
        String study = (String)daf.get("study");
        String idcard = (String)daf.get("idcard");
        String birthday = (String)daf.get("birthday");
        String entry = (String)daf.get("entry");
        String listState = request.getParameter("listState");
        char sex = daf.get("sex").toString().trim().charAt(0);
        char state = daf.get("state").toString().trim().charAt(0);
        String tel = (String)daf.get("tel");
        String mobil = (String)daf.get("mobil");
        try
        {
            u = user.findUserById(id);
            u.setAddress(address);
            u.setBirthday(birthday);
            u.setEmail(email);
            u.setEntry(entry);
            u.setIdcard(idcard);
            u.setMobil(mobil);
            u.setTel(tel);
            u.setName(name);
            u.setSex(sex);
            u.setState(state);
            u.setStudy(study);
            u = user.save(u);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("state", listState);
        request.setAttribute("pageId", page);
        request.setAttribute("user", u);
        request.setAttribute("type", "edit");
        if(src != null)
            return mapping.findForward(src);
        else
            return mapping.findForward("editOk");
    }

    public ActionForward addUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        daf = (DynaActionForm)form;
        User u = new User();
        String id = (String)daf.get("id");
        String userId = (String)daf.get("userId");
        String name = (String)daf.get("name");
        String password = (String)daf.get("password");
        String email = (String)daf.get("email");
        String address = (String)daf.get("address");
        String study = (String)daf.get("study");
        String idcard = (String)daf.get("idcard");
        String birthday = (String)daf.get("birthday");
        String entry = (String)daf.get("entry");
        char sex = daf.get("sex").toString().trim().charAt(0);
        String state = request.getParameter("state");
        String tel = "";
        String mobil = "";
        if((String)daf.get("tel") != null)
            tel = (String)daf.get("tel");
        if((String)daf.get("mobil") != null)
            mobil = (String)daf.get("mobil");
        try
        {
            Organize org = null;
            if(id != null && !id.equals(""))
                org = organize.findOrganizeById(id);
            else
                org = organize.findRoot();
            u.setUserId(userId);
            u.setName(name);
            u.setPassword(password);
            u.setAddress(address);
            u.setBirthday(birthday);
            u.setEmail(email);
            u.setEntry(entry);
            u.setIdcard(idcard);
            u.setMobil(mobil);
            u.setTel(tel);
            //u.setName(name);
            u.setSex(sex);
            u.setState(state.trim().charAt(0));
            u.setStudy(study);
            u = organize.addUser(org, u);
            role.setRole(role.findStudent(), u);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("state", state);
        request.setAttribute("pageId", "1");
        request.setAttribute("type", "create");
        request.setAttribute("user", u);
        return mapping.findForward("editOk");
    }

    public ActionForward userMenuDocument(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String strRole = request.getParameter("role");
        org.dom4j.Document doc = null;
        try
        {
            String user_id = ((UserSession)request.getSession().getAttribute("userSession")).getId();
            doc = user.getUserMenuDocument(user_id, strRole);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        response.setContentType("text/xml; charset=UTF-8");
        javax.servlet.ServletOutputStream sos = response.getOutputStream();
        XMLWriter writer = new XMLWriter(sos);
        writer.write(doc);
        writer.flush();
        writer.close();
        return null;
    }

    public ActionForward exportTemplate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String name = request.getRealPath("/") + "template/user.xls";
        List list;
        try
        {
            UserSession userSession = (UserSession)request.getSession().getAttribute("userSession");
            String manageId = userSession.getManageOrgId();
            list = organize.findAllChildOrganize(organize.findOrganizeById(manageId));
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("list", list);
        request.setAttribute("name", name);
        return mapping.findForward("user_template");
    }

    public ActionForward importUserInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        daf = (DynaActionForm)form;
        String dataFile = (String)daf.get("name");
        logger.info(dataFile);
        List list = null;
        try
        {
            list = excel.readExcel(dataFile);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        for(int i = 0; i < list.size(); i++)
        {
            User u = new User();
            List temp = (List)list.get(i);
            u.setUserId(temp.get(0).toString().trim());
            u.setName(temp.get(1).toString().trim());
            if(temp.get(2).equals("\u7537"))
                u.setSex('f');
            else
            if(temp.get(2).equals("\u5973"))
                u.setSex('m');
            u.setPassword(temp.get(3).toString().trim());
            try
            {
                u.setOrganizeId(organize.findOrganizeByUrl((String)temp.get(4)).getId());
                if(temp.get(5).equals("\u6B63\u5E38\u7528\u6237"))
                    u.setState('a');
                else
                if(temp.get(5).equals("\u6682\u505C"))
                    u.setState('p');
                else
                if(temp.get(5).equals("\u5DF2\u5220\u9664"))
                    u.setState('g');
                u.setEmail(temp.get(6) == null ? null : temp.get(6).toString().trim());
                u.setTel(temp.get(7) == null ? null : temp.get(7).toString().trim());
                u.setMobil(temp.get(8) == null ? null : temp.get(8).toString().trim());
                u.setAddress(temp.get(9) == null ? null : temp.get(9).toString().trim());
                u.setIdcard(temp.get(10).toString().trim());
                u.setBirthday(temp.get(11) == null ? null : temp.get(11).toString().trim());
                u.setStudy(temp.get(12) == null ? null : temp.get(12).toString().trim());
                u.setEntry(temp.get(13) == null ? null : temp.get(13).toString().trim());
                u = user.save(u);
                role.setRole(role.findStudent(), u);
            }
            catch(BusinessException e)
            {
                e.printStackTrace();
                request.setAttribute("exception", e);
                return mapping.findForward("failure");
            }
            logger.info("\u5BFC\u5165\u7528\u6237: " + u.getName() + "[" + u.getUserId() + "] \u6210\u529F!");
        }

        return mapping.findForward("import_complete");
    }

    private static Logger logger;
    private DynaActionForm daf;
    static Class class$0; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.action.organize.UserTxAction.class.getName());
    }
}
